﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.Answer
{
    public partial class Answer : HFController.BagePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员有没有登录，如果没登录，踢出
            this.checkAdminIsLogin();

            //是否首次加载
            if (!Page.IsPostBack)
            {
                //绑定提问
                this.bindAsk();

                //绑定律师
                this.bindLawyer();

                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        AnswerController.getController().doDelegeAnswer(ID);
                        break;
                }
            }
        }

        /// <summary>
        /// 绑定提问
        /// </summary>
        public void bindAsk()
        {
            dropAskID.DataSource = AskController.getController().getAskList(100);
            dropAskID.DataValueField = "ID";
            dropAskID.DataTextField = "Title";
            dropAskID.DataBind(); //执行绑定
            dropAskID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定律师
        /// </summary>
        public void bindLawyer()
        {
            DropLawyerID.DataSource = LawyerController.getController().getLawyerList(100);
            DropLawyerID.DataValueField = "ID";
            DropLawyerID.DataTextField = "UID";
            DropLawyerID.DataBind();
            DropLawyerID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定需要修改的数据
        /// (就是点击修改某条记录，把上面的修改文本的信息，设置成你点的这条记录)
        /// </summary>
        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            //得到这条记录
            hf.Model.Answer model = AnswerController.getController().getAnswerModel(ID);
            //输入框赋值
            dropAskID.SelectedValue = model.AskID.ToString();
            DropLawyerID.SelectedValue = model.LawyerID.ToString();
            tbContent.Text = model.Content;
            tbSecretContent.Text = model.SecretContent;
            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //获取Action参数，判断是修改还是保存
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                //修改记录，刚才bindmodel()方法里，赋值的那两个
                AnswerController.getController().doModifyAnswer(int.Parse(IDS.Value),
                    int.Parse(dropAskID.SelectedValue),
                    int.Parse(DropLawyerID.SelectedValue), 
                    tbContent.Text, 
                    tbSecretContent.Text);
            }
            else
            {
                AnswerController.getController().doAddAnswer(int.Parse(dropAskID.SelectedValue), 
                    tbContent.Text, 
                    int.Parse(DropLawyerID.SelectedValue), 
                    tbSecretContent.Text);
                
            }
        }
    }
}